package com.interfaces.module.pay.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.interfaces.aspect.auth.LoginAuth;
import com.interfaces.module.order.domain.FtsOrder;
import com.interfaces.module.order.service.FtsOrderService;
import com.interfaces.module.user.domain.FtsUser;
import com.interfaces.util.Result;
import com.interfaces.util.TokenUtil;
import com.interfaces.wx.pay.WxJsapiService;
import com.wechat.pay.java.service.payments.jsapi.model.PrepayWithRequestPaymentResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
@RestController
@RequestMapping("/wxpay")
public class WxPayController  {
    @Autowired
    private FtsOrderService ftsOrderService;
    /**
     * 获取支付条件拼接参数,并进行微信下单
     * @return
     */
//    @LoginAuth(flag = true)
    @RequestMapping("/prePayOrder")
    public Result prePayOrder(@RequestBody FtsOrder ftsOrder){
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("order_code",ftsOrder.getOrderCode());
        FtsOrder ftsOrders=ftsOrderService.getOne(queryWrapper);
        //待支付订单
        ftsOrders.setState(1);
        boolean flag=ftsOrderService.updateById(ftsOrders);
        PrepayWithRequestPaymentResponse paymentResponse =WxJsapiService.prepay(ftsOrders);
        if(flag){
            return Result.SUCCESS(paymentResponse);
        }else{
            return Result.ERROR("下单失败");
        }

    }
    /**
     * 取消订单
     * @return
     */
    @LoginAuth
    @RequestMapping("/cancelOrder")
    public Result cancelOrder(String orderCode){
        WxJsapiService.closeOrder(orderCode);
        return Result.SUCCESS("");
    }
    /**
     * 微信异步回调通知
     * @return
     */
    @LoginAuth
    @RequestMapping("/callback")
    public Map wxNotify(Map map){
        Map result=new HashMap();
        if (!"TRANSACTION.SUCCESS".equals(map.get("event_type"))) {
            result.put("code", 500);
            result.put("message", "异步通知状态码异常");
            return result;
        }
        //进行解析验证支付异步回调数据
        return result;

    }
}
